package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.GoodsBean;
import com.saifan.wyy_ov.data.bean.GroupBuyBean;
import com.saifan.wyy_ov.data.bean.GroupBuyBean.GoodsList;
import com.saifan.wyy_ov.utils.ImgUtil;

public class AdapterPromotionItem extends BaseAdapter {
    Context context;
    List<GoodsList> list;
    int showCount = 2;
    private GroupBuyBean groupBuyBean;

    public int getShowCount() {
        return showCount;
    }

    public void setShowCount(int showCount) {
        this.showCount = showCount;
        notifyDataSetChanged();
    }

    public AdapterPromotionItem(Context context, GroupBuyBean groupBuyBean, List<GoodsList> list) {

        this.context = context;
        this.list = list;
        this.groupBuyBean = groupBuyBean;
    }

    @Override
    public int getCount() {
       
        if ( list!=null&& !list.isEmpty()) {
            if (list.size() < showCount) {
                return list.size();
            }
            return showCount;
        } else
            return 0;
    }

    @Override
    public Object getItem(int position) {
       
        if (list!=null && !list.isEmpty())
            return list.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
       
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder(context);
            convertView = viewHolder.converView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setGoodsBean(list.get(position));
        return convertView;
    }

    static class ViewHolder {
        // Content View Elements
        public ImageView goods_img;
        public TextView tv_goods_name;
        public TextView tv_time;
        public TextView price1,price2,sy;

        View converView;
        GoodsList goodsBean;
        Context context;

        // End Of Content View Elements
        public GoodsList getGoodsBean() {
            return goodsBean;
        }

        public void setGoodsBean(GoodsList goodsBean  ) {
            this.goodsBean = goodsBean;

            String startTime = goodsBean.getGroupBuyingStartTime().substring(0, 10) + " " + goodsBean.getGroupBuyingStartTime().substring(11, 16);
            String endTime = goodsBean.getGroupBuyingEndTime().substring(0, 10) + " " + goodsBean.getGroupBuyingEndTime().substring(11, 16);
            tv_time.setText(startTime + " 至 " + endTime);
            ImgUtil.DisPlayImg(goodsBean.getCoverPhoto(), goods_img, context);

            sy.setText(goodsBean.getGroupBuyingRemainingCount()+"");
            price1.setText(context.getString(R.string.rmb,goodsBean.getGroupBuyingPrice()+""));
            price2.setText(context.getString(R.string.rmb,goodsBean.getGoodsPrice()+""));
            tv_goods_name.setText(goodsBean.getGoodsName());
            price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }

        public ViewHolder(Context context) {

            this.context=context;
            converView = LayoutInflater.from(context).inflate(R.layout.promotion_goods_item, null);
            goods_img = (ImageView) converView.findViewById(R.id.goods_img);
            tv_goods_name = (TextView) converView.findViewById(R.id.tv_goods_name);
            tv_time = (TextView) converView.findViewById(R.id.tv_time);
            price1 = (TextView) converView.findViewById(R.id.price1);
            price2 = (TextView) converView.findViewById(R.id.price2);
            sy = (TextView) converView.findViewById(R.id.sy);
        }
    }
}
